package cn.stylefeng.guns.modular.fileAssociation.controller;

import cn.stylefeng.guns.base.pojo.page.LayuiPageInfo;
import cn.stylefeng.guns.modular.file.entity.File;
import cn.stylefeng.guns.modular.fileAssociation.entity.FileAssociation;
import cn.stylefeng.guns.modular.fileAssociation.model.params.FileAssociationParam;
import cn.stylefeng.guns.modular.fileAssociation.service.FileAssociationService;
import cn.stylefeng.roses.core.base.controller.BaseController;
import cn.stylefeng.roses.core.reqres.response.ResponseData;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;


/**
 * 文件关联控制器
 *
 * @author jalor
 * @Date 2019-10-21 21:43:55
 */
@Controller
@RequestMapping("/fileAssociation")
@Api(description = "文件关联控制器")
public class FileAssociationController extends BaseController {

    private String PREFIX = "/fileAssociation";

    @Autowired
    private FileAssociationService fileAssociationService;

    /**
     * 跳转到主页面
     *
     * @author jalor
     * @Date 2019-10-21
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "/fileAssociation.html";
    }

    /**
     * 新增页面
     *
     * @author jalor
     * @Date 2019-10-21
     */
    @RequestMapping("/add")
    public String add() {
        return PREFIX + "/fileAssociation_add.html";
    }

    /**
     * 编辑页面
     *
     * @author jalor
     * @Date 2019-10-21
     */
    @RequestMapping("/edit")
    public String edit() {
        return PREFIX + "/fileAssociation_edit.html";
    }

    /**
     * 新增接口
     *
     * @author jalor
     * @Date 2019-10-21
     */
    @RequestMapping("/addItem")
    @ResponseBody
    public ResponseData addItem(FileAssociationParam fileAssociationParam) {
        this.fileAssociationService.add(fileAssociationParam);
        return ResponseData.success();
    }

    /**
     * 编辑接口
     *
     * @author jalor
     * @Date 2019-10-21
     */
    @RequestMapping("/editItem")
    @ResponseBody
    public ResponseData editItem(FileAssociationParam fileAssociationParam) {
        this.fileAssociationService.update(fileAssociationParam);
        return ResponseData.success();
    }

    /**
     * 删除接口
     *
     * @author jalor
     * @Date 2019-10-21
     */
    @RequestMapping("/delete")
    @ResponseBody
    public ResponseData delete(FileAssociationParam fileAssociationParam) {
        this.fileAssociationService.delete(fileAssociationParam);
        return ResponseData.success();
    }

    /**
     * 查看详情接口
     *
     * @author jalor
     * @Date 2019-10-21
     */
    @RequestMapping("/detail")
    @ResponseBody
    public ResponseData detail(FileAssociationParam fileAssociationParam) {
        FileAssociation detail = this.fileAssociationService.getById(fileAssociationParam.getId());
        return ResponseData.success(detail);
    }

    /**
     * 查询列表
     *
     * @author jalor
     * @Date 2019-10-21
     */
    @ResponseBody
    @RequestMapping("/list")
    public LayuiPageInfo list(FileAssociationParam fileAssociationParam) {
        return this.fileAssociationService.findPageBySpec(fileAssociationParam);
    }

    @ResponseBody
    @PostMapping("/listFile")
    @ApiOperation("根据源ID[产品/功能/需求/里程碑]获取文件集合")
    public List<File> listFile(@ApiParam(value = "类型[1=产品,2=功能,3=需求,4=里程碑]", required = true) @RequestParam Integer type,
                               @ApiParam(value = "源ID", required = true) @RequestParam Long sourceId) {
        return fileAssociationService.listFile(type, sourceId);
    }

}


